package com.dtjd.playcoinmonkey.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dtjd.playcoinmonkey.ChildClick;
import com.dtjd.playcoinmonkey.bean.CommentBean;
import com.dtjd.playcoinmonkey.bean.CommentDetailBean;
import com.dtjd.playcoinmonkey.bean.ReplyDetailBean;

import java.util.List;
import com.dtjd.playcoinmonkey.R;
public class CommentExpandAdapter extends BaseExpandableListAdapter {
    private final List<CommentBean> commentBeanList;
    private final Context context;
    private final ChildClick click;

    public CommentExpandAdapter(Context context, List<CommentBean> commentBeanList, ChildClick click) {
        this.context = context;
        this.commentBeanList = commentBeanList;
        this.click = click;
    }
    @Override
    public int getGroupCount() {
        return commentBeanList.size();
    }
    @Override
    public int getChildrenCount(int i) {
      return commentBeanList.get(i).replyDetailBeans.size();
    }
    @Override
    public Object getGroup(int i) {
        return commentBeanList.get(i);
    }
    @Override
    public Object getChild(int i, int i1) {
        return commentBeanList.get(i).replyDetailBeans.get(i1);
    }
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return getCombinedChildId(groupPosition, childPosition);
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }
    @SuppressLint("StringFormatMatches")
    @Override
    public View getGroupView(final int groupPosition, boolean isExpand, View convertView, ViewGroup viewGroup) {
        CommentDetailBean bean = commentBeanList.get(groupPosition).commentDetailBean;
        final GroupHolder groupHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment, viewGroup, false);
            groupHolder = new GroupHolder(convertView);
            convertView.setTag(groupHolder);
        }else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
        Glide.with(context).load(bean.wb_url).placeholder(R.drawable.loading).apply(RequestOptions.bitmapTransform(new CircleCrop())).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(groupHolder.logo);
        groupHolder.nickname.setText(bean.wb_nickname);
        groupHolder.content.setText(bean.wc_content);
        groupHolder.reply.setText(String.format(context.getResources().getString(R.string.reply),bean.reply_number));
        groupHolder.time.setText(bean.time);
        groupHolder.clickToReply.setOnClickListener(view -> click.click(view,groupPosition));
        return convertView;
    }
    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
        ReplyDetailBean bean = commentBeanList.get(groupPosition).replyDetailBeans.get(childPosition);
        final ChildHolder childHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_reply,viewGroup, false);
            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        childHolder.nickname1.setText(bean.wb_nickname);
        if (bean.reply_nickname.trim().equals("")){
            childHolder.say.setText(":");
        }else{
            childHolder.say.setText(context.getResources().getString(R.string.say));
            childHolder.nickname2.setText(bean.reply_nickname);
        }
        childHolder.content.setText(bean.reply_content);
        return convertView;
    }
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    private static class GroupHolder{
        private final ImageView logo;
        private final TextView nickname, content, time,reply,clickToReply;
        public GroupHolder(View view) {
            logo = view.findViewById(R.id.item_comment_icon);
            nickname = view.findViewById(R.id.item_comment_nickname);
            time = view.findViewById(R.id.item_comment_time);
            reply = view.findViewById(R.id.item_comment_reply);
            content = view.findViewById(R.id.item_comment_content);
            clickToReply= view.findViewById(R.id.item_comment_replyClick);
        }
    }
    private static class ChildHolder{
        private final TextView nickname1, nickname2,content,say;
        public ChildHolder(View view) {
            nickname1 = view.findViewById(R.id.item_reply_nickname);
            nickname2 = view.findViewById(R.id.item_reply_nickname2);
            say = view.findViewById(R.id.item_reply_say);
            content = view.findViewById(R.id.item_reply_content);
        }
    }
}